﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;
using Sweepids.Models.Object;

namespace Sweepids.Models.Dao
{
    public class UserDao : Dao
    {
        public void Create(User user)
        {
            try
            {
                try
                {
                    var myCommand = new OracleCommand("INSERT INTO SWEEP_USER " +
                                                      "(USERNAME, CREDIT_CARD, EMAIL, FIRST_NAME, LAST_NAME, ADDRESS, ZIP, SWEEPS_OWNED, IS_ADMIN, PASSWORD) " +
                                                      "VALUES " +
                                                      "(:USERNAME, :CREDIT_CARD, :EMAIL, :FIRST_NAME, :LAST_NAME, :ADDRESS, :ZIP, :SWEEPS_OWNED, :IS_ADMIN, :PASSWORD)", myConnection);
                    RunCommand(myCommand, user.OracleParameters, false);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("CreditCardDao::Create(CreditCard): " + e.Message);
                    throw;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("UserDao::Create(User): " + e.Message);
                throw;
            }
        }

        public User Read(string username)
        {
            try
            {

                var result = new User { Username = username };
                var myCommand = new OracleCommand("SELECT * FROM SWEEP_USER, SWEEP_ZIP " +
                                                  "WHERE SWEEP_USER.ZIP=SWEEP_ZIP.ZIP " +
                                                  "AND USERNAME=:USERNAME", myConnection);
                var myReader = RunCommand(myCommand, result.OracleParameters, true);
                while (myReader.Read())
                {
                    result = new User(myReader);
                }
                myReader.Close();
                myConnection.Close();
                return result;
            }
            catch (Exception e)
            {
                Debug.WriteLine("UserDao::Read(string): " + e.Message);
                throw;
            }
        }

        public List<User> ReadAll()
        {
            try
            {
                var results = new List<User>();
                var myCommand = new OracleCommand("SELECT * FROM SWEEP_USER ", myConnection);
                var myReader = RunCommand(myCommand, true);
                while (myReader.Read())
                {
                    results.Add(new User(myReader));
                }
                myReader.Close();
                myConnection.Close();
                return results;
            }
            catch (Exception e)
            {
                Debug.WriteLine("UserDao::ReadAll(): " + e.Message);
                throw;
            }
        }

        public void Update(User user)
        {
            try
            {
                var myCommand = new OracleCommand("UPDATE SWEEP_USER SET " +
                                                  "USERNAME=:USERNAME, " +
                                                  "CREDIT_CARD=:CREDIT_CARD, " +
                                                  "EMAIL=:EMAIL, " +
                                                  "FIRST_NAME=:FIRST_NAME, " +
                                                  "LAST_NAME=:LAST_NAME, " +
                                                  "ADDRESS=:ADDRESS, " +
                                                  "ZIP=:ZIP, " +
                                                  "SWEEPS_OWNED=:SWEEPS_OWNED, " +
                                                  "IS_ADMIN=:IS_ADMIN, " +
                                                  "PASSWORD=:PASSWORD " +
                                                  "WHERE USERNAME=:USERNAME", myConnection);
                RunCommand(myCommand, user.OracleParameters, false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("UserDao::Update(User): " + e.Message);
                throw;
            }
        }

        public void Delete(User user)
        {
            try
            {
                var myCommand = new OracleCommand("DELETE FROM SWEEP_USER " +
                                                 "WHERE USERNAME=:USERNAME", myConnection);
                RunCommand(myCommand, user.OracleParameters, false);
                //Delete credit card with user
                var creditCardDao = new CreditCardDao();
                creditCardDao.Delete(user.CreditCardObj);
            }
            catch (Exception e)
            {
                Debug.WriteLine("UserDao::Delete(User): " + e.Message);
                throw;
            }
        }
    }
}